import os
from time import sleep

from selenium.webdriver.common.by import By
from common.base import Base
from common.functions import open_browser


class GoodsManagement(Base):
    order_management_url = 'http://139.129.26.163/admin'

    # 点击商品管理标签
    def click_goods_management(self):
        self.base_click((By.XPATH, '//div[@id="menu-list"]/ul/li[2]'))

    # 点击添加新商品标签
    def click_add_new_goods(self):
        self.base_click((By.LINK_TEXT, '添加新商品'))

    # 点击通用信息
    def click_general(self):
        self.base_switch_default_iframe()
        self.base_switch_iframe((By.ID, 'main-frame'))
        self.base_click((By.ID, 'general-tab'))

    # 输入商品名称
    def input_goods_name(self, content):
        self.base_send_keys((By.NAME, 'goods_name'), content)

    # 输入字体颜色
    def input_font_color(self, num1, num2):
        self.base_click((By.ID, 'font_color'))
        self.base_click((By.XPATH, f'//div[@id="ColorSelectertBox"]/table/tbody/tr[{num1}]/td[{num2}]'))

    # 输入字体样式
    def input_font_style(self, content):
        self.base_select_value((By.NAME, 'goods_name_style'), content)

    # 输入商品货号
    def input_goods_sn(self, content):
        self.base_send_keys((By.NAME, 'goods_sn'), content)

    # 商品分类
    def goods_classify(self, content):
        self.base_select_value((By.XPATH, '//*[@id="general-table"]/tbody/tr[3]/td[2]/select'), content)

    # 添加分类
    def add_classify(self, content):
        self.base_click((By.XPATH, '//table[@id="general-table"]/tbody/tr[3]/td[2]/button'))
        self.base_send_keys((By.NAME, 'addedCategoryName'), content)
        self.base_click((By.XPATH, '//span[@id="category_add"]/button[1]'))

    # 扩展分类-添加
    def extend_sort(self, content):
        self.base_click((By.XPATH, "//input[@value='添加']"))
        sleep(1)
        self.base_select_value((By.NAME, "other_cat[]"), content)

    # 商品品牌-选择
    def brand(self, content):
        self.base_select_value((By.NAME, "brand_id"), content)

    # 商品品牌-添加品牌
    def insert_brand(self, content):
        self.base_click(((By.XPATH, "//table[@id='general-table']/tbody/tr[5]/td[2]/button")))
        sleep(1)
        self.base_send_keys((By.NAME, "addedBrandName"), content)
        sleep(1)
        self.base_click((By.XPATH, "//span[@id='brand_add']/button"))
        sleep(1)

    # 选择供货商
    def supplier(self, content):
        self.base_select_value((By.ID, "suppliers_id"), content)

    # 本店售价
    def shop_price(self, content):
        self.base_select_all((By.NAME, "shop_price"))
        self.base_send_keys((By.NAME, "shop_price"), content)

    # 本店售价-按市场价计算
    def market_calc(self):
        self.base_click((By.XPATH, "//input[@value='按市场价计算']"))

    # 会员价格-注册用户
    def register_price(self, content):
        self.base_select_all((By.ID, "rank_1"))
        self.base_send_keys((By.ID, "rank_1"), content)

    # 会员价格-代销用户
    def cons_price(self, content):
        # 代销用户
        self.base_select_all((By.ID, "rank_3"))
        self.base_send_keys((By.ID, "rank_3"), content)

    # 会员价格-vip用户
    def vip_price(self, content):
        self.base_select_all((By.ID, "rank_2"))
        self.base_send_keys((By.ID, "rank_2"), content)

    # 商品优惠价格-优惠数量
    def volume_number(self, content):
        self.base_send_keys((By.NAME, "volume_number[]"), content)

    # 商品优惠价格-优惠价格
    def volume_price(self, content):
        self.base_send_keys((By.NAME, "volume_price[]"), content)

    # 市场售价
    def market_price(self, content):
        self.base_select_all((By.NAME, "market_price"))
        self.base_send_keys((By.NAME, "market_price"), content)

    # 取整数
    def rounding(self):
        self.base_click((By.XPATH, "//input[@value='取整数']"))

    # 虚拟销量
    def virtual_sales(self, content):
        self.base_select_all((By.NAME, "virtual_sales"))
        self.base_send_keys((By.NAME, "virtual_sales"), content)

    # 赠送消费积分
    def give_integral(self, content):
        self.base_select_all((By.NAME, "give_integral"))
        self.base_send_keys((By.NAME, "give_integral"), content)

    # 赠送等级积分数
    def rank_integral(self, content):
        self.base_select_all((By.NAME, "rank_integral"))
        self.base_send_keys((By.NAME, "rank_integral"), content)

    # 积分购买金额
    def integral(self, content):
        self.base_select_all((By.NAME, "integral"))
        self.base_send_keys((By.NAME, "integral"), content)

    # 促销价打勾并填写价格
    def click_promote(self, content):
        self.base_click((By.ID, 'is_promote'))
        self.base_select_all((By.ID, "promote_1"))
        self.base_send_keys((By.ID, "promote_1"), content)

    # 促销日期
    # 促销日期起始日期
    def promote_start_date(self, content):
        js_date = "document.getElementById('promote_start_date').removeAttribute('readonly', '')"
        self.base_execute_script(js_date)
        sleep(1)
        self.base_select_all((By.ID, "promote_start_date"))
        self.base_send_keys((By.ID, "promote_start_date"), content)
        sleep(1)

    # 促销日期结束日期
    def promote_end_date(self, content):
        js_date = "document.getElementById('promote_end_date').removeAttribute('readonly', '')"
        self.base_execute_script(js_date)
        sleep(1)
        self.base_select_all((By.ID, "promote_end_date"))
        self.base_send_keys((By.ID, "promote_end_date"), content)
        sleep(1)

    # 上传本地商品图片
    def upload_goods_img(self,content):
        aotoframe_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
        file = os.path.join(os.path.abspath(aotoframe_path), 'image', f'{content}')
        self.base_send_keys((By.NAME, 'goods_img'), file)

    # 上传商品图片-网络图片
    def goods_img_url(self, content):
        self.base_send_keys((By.NAME, "goods_img_url"), content)

    # 上传本地商品缩略图
    def goods_thumbs(self, content):
        aotoframe_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
        file = os.path.join(os.path.abspath(aotoframe_path), 'image', f'{content}')
        self.base_send_keys((By.NAME, "goods_thumbs"), file)

    # 上传商品缩略图-网络图片
    def goods_thumbs_url(self, content):
        self.base_send_keys((By.NAME, "goods_thumb_url"), content)

    # 勾选自动生成商品缩略图
    def auto_thumb(self):
        self.base_click((By.ID, "auto_thumb"))

    # 确定按钮
    def sure(self):
        self.base_click((By.XPATH, "//div[@class='button-div']/input[2]"))

    # 重置按钮
    def reset(self):
        self.base_click((By.XPATH, "//div[@class='button-div']/input[3]"))


